package api

import (
	"awesomeProject2/global"
	"awesomeProject2/model"
	"awesomeProject2/model/request"
	"awesomeProject2/model/response"
	"awesomeProject2/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"gorm.io/gorm"
	"net/http"
	"strconv"
	"strings"
)

type Work struct {
	gorm.Model
	NumberOfWords float64  `json:"numberOfWords"`
	PubName       string   `json:"pubName"`
	PubTime       string   `json:"pubTime"`
	WorkCategory  string   `json:"workCategory"`
	WorkFile      []string `json:"workFile"`
	WorkLevel     string   `json:"workLevel"`
	WorkName      string   `json:"workName"`
	WorkRanking   int      `json:"workRanking"`
	UserId        uint     `json:"userid"  `
	Year          int      `json:"year"`
}

func CreateWork(c *gin.Context) {
	var work model.Work
	userid, _ := c.Get("userid")
	var data Work
	_ = c.ShouldBindJSON(&data)
	//Year := model.GetYear()
	work = model.Work{
		WorkName:      data.WorkName,
		PubName:       data.PubName,
		PubTime:       data.PubTime,
		WorkCategory:  data.WorkCategory,
		WorkLevel:     data.WorkLevel,
		NumberOfWords: data.NumberOfWords,
		WorkRanking:   data.WorkRanking,
		WorkFile:      strings.Join(data.WorkFile, ","),
		Year:          data.Year,
		UserId:        userid.(uint),
	}
	if err := model.CreateWork(work); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":    "添加失败",
			"status": http.StatusNotFound,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"meta": gin.H{
				"msg":    "添加成功",
				"status": http.StatusOK,
			},
		})
	}
}
func GetWork(c *gin.Context) {
	wkid, _ := strconv.Atoi(c.Param("id"))
	if err, data := model.GetWork(uint(wkid)); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":    "查询失败",
			"status": http.StatusNotFound,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"data": data,
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			}},
		)
	}
}
func GetWorks(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pagesize"))
	pageNum, _ := strconv.Atoi(c.Query("pagenum"))
	userid, _ := strconv.Atoi(c.Query("userid"))
	if pageSize == 0 {
		pageSize = -1
	}
	if pageNum == 0 {
		pageNum = 1
	}
	if err, works, total := model.GetWorks(uint(userid), pageSize, pageNum); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":    "查询失败",
			"status": http.StatusNotFound,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"data":  gin.H{"workList": works},
			"tatal": total,
			"meta":  gin.H{"msg": "查询成功", "status": http.StatusOK},
		})
	}
}
func UpdateWork(c *gin.Context) {
	var data Work
	_ = c.ShouldBindJSON(&data)
	//Year := model.GetYear()
	work := model.Work{
		WorkName:      data.WorkName,
		PubName:       data.PubName,
		PubTime:       data.PubTime,
		WorkCategory:  data.WorkCategory,
		WorkLevel:     data.WorkLevel,
		NumberOfWords: data.NumberOfWords,
		WorkRanking:   data.WorkRanking,
		WorkFile:      strings.Join(data.WorkFile, ","),
		Year:          data.Year,
	}
	work.ID = data.ID
	if err := model.UpdateWork(work); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "更新失败", "status": http.StatusNotFound})

	} else {
		c.JSON(http.StatusOK, gin.H{"meta": gin.H{"msg": "更新成功", "status": http.StatusOK}})
	}
}
func DeleteWork(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	if err := model.DeleteWork(id); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "删除失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{"meta": gin.H{"msg": "删除成功", "status": http.StatusOK}})
	}
}
func ChangeWorkVisiblity(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	if err := model.ChangeWorkVisiblity(uint(id)); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "更新失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{"meta": gin.H{"msg": "更新成功", "status": http.StatusOK}})
	}
}
func GetAllWorks(c *gin.Context) {

	userid, _ := c.Get("userid")
	year, _ := strconv.Atoi(c.Param("year"))
	if err, data, tt := model.GetAllWorks(userid.(uint), year); err != nil {

		c.JSON(http.StatusOK, gin.H{"msg": userid.(uint), "status": http.StatusNotFound})
	} else {

		c.JSON(http.StatusOK, gin.H{
			"data": gin.H{"workList": data, "total": tt},
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			},
		})
	}
}

func GetAllWorksWithoutYear(c *gin.Context) {

	userid, _ := c.Get("userid")

	if err, data, tt := model.GetAllWorks3(userid.(uint)); err != nil {

		c.JSON(http.StatusOK, gin.H{"msg": userid.(uint), "status": http.StatusNotFound})
	} else {

		c.JSON(http.StatusOK, gin.H{
			"data": gin.H{"workList": data, "total": tt},
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			},
		})
	}
}

func GetWorksDynamic(c *gin.Context) {
	var searchinfo request.WorkSearchInfo
	_ = c.ShouldBindQuery(&searchinfo)
	if err, list, total := service.GetWorksDynamic(searchinfo); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.Fail(c)
	} else {
		response.OkWithData(response.PageResult{
			Total: total,
			List:  list,
		}, c)
	}
}
